
/**
 * @apiDefine ErrorResponse
 * @apiError (请求错误) {String} err_message 错误信息描述
 */

/**
 * @apiVersion 1.0.0
 * @apiGroup  轮播图
 * @api {POST} api/banner/delete 删除轮播图
 * @apiName api/banner/delete
 * @apiExample {js} axios 例子:
 *  axios.post('http://39.105.38.34:8080/api/banner/delete', {
 *    id: 1,
 *  });
 * @apiHeader {String} authorization 用户登录或注册成功时返回的 token
 * @apiParam {Number} id 图片id
 * @apiSuccess (请求成功) {Null} data 删除成功后不返回任何数据
 * @apiuse  ErrorResponse
 */

const { query, del } = require('../../helper/db');
const { ok, door } = require('../../helper/util');

module.exports = async ctx => {
  var token = ctx.headers.authorization;
  var params = ctx.request.body;
  console.log(params.id);

  await door(token);
  var bannerlist = await query('select * from banner where id = ?', [params.id]);

  if (!bannerlist.length) {
    throw new Error('图片不存在');
  }

  await del('banner', params.id);
  
  ctx.body = ok(null);
}
